/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月26日         luwei
 */

package com.coocaa.launcher.wallpaper;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.launcher.LauncherBaseActivity;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperList;
import com.tianci.appstore.R;

public class WallpaperPreViewActivity extends LauncherBaseActivity {
    private WallpaperPreViewLayout mPreViewLayout = null;
    private boolean isDestory = false;
    private int pos = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus && mPreViewLayout == null) {
            String json = getIntent().getStringExtra("item");
            pos = getIntent().getIntExtra("pos", 0);
            RP_WallpaperList.WallPaper_Item item = JSONObject.parseObject(json, RP_WallpaperList.WallPaper_Item.class);
            mPreViewLayout = new WallpaperPreViewLayout(this);
            setContentView(mPreViewLayout);
            mPreViewLayout.setData(item);
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
            animation.setDuration(1000);
            mPreViewLayout.startAnimation(animation);
        }
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    show(keyCode);
                    break;
                case KeyEvent.KEYCODE_DPAD_UP:
                    show(keyCode);
                    break;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void show(int keycode) {
        if (mPreViewLayout != null) {
            RP_WallpaperList.WallPaper_Item item = null;
            item = mPreViewLayout.getData();
            if (keycode == KeyEvent.KEYCODE_DPAD_DOWN) {
                pos++;
            } else {
                pos--;
            }
            if (pos > -1) {
                item = WallpaperController.getInstance(this).getWallpaperItem(pos);
                if (item != null)
                    mPreViewLayout.setData(item);
                else
                    pos--;
            } else {
                pos++;
            }
        }
    }

    @Override
    protected void onDestroy() {
        mPreViewLayout.destory();
        mPreViewLayout = null;
        super.onDestroy();
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "电视派壁纸预览页面";
    }
}
